﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Threading;
using System.Collections.Concurrent;
using mds.NoSqlDatabaseService;


namespace mds.DFSWebService
{
    public class FileWebCache
    {
        private static FileWebCache _instance = new FileWebCache();

        public static FileWebCache Instance
        {
            get { return _instance; }           
        } 
        private Cache _cache;
        private FileWebCache()
        {
            _cache = HttpRuntime.Cache;
        }
        private void AddCache(Object stateInfo)
        {
            var data = (DFSFile)stateInfo;
            _cache.Add(data.Id.ToString(), data, null, Cache.NoAbsoluteExpiration, new TimeSpan(1, 0, 0), CacheItemPriority.Low, null);
        }
        private void AddCacheDownload(Object stateInfo)
        {
            var data = (DFSFile)stateInfo;
            _cache.Add(data.Id.ToString(), data, null, DateTime.Now.AddMinutes(2.0), Cache.NoSlidingExpiration, CacheItemPriority.Low, null);
        }
        public void Store(DFSFile data)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(AddCache), data);
        }
        public DFSFile Get(string dfsId)
        {
            object data=_cache[dfsId];
            if (data != null)
                return (DFSFile)data;
            else
                return null;
        }

        internal void StoreDownload(DFSFile data)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(AddCacheDownload), data);
        }
    }
}